import { parse } from 'url';
// mock tableListDataSource
let tableListDataSource = [];

for (let i = 0; i < 10; i += 1) {
  tableListDataSource.push({
    key: i,
    accidentStatus: '处理中',//状态
    accidentType:'内件缺失',//类型
    flightNo:'CA123',//航班号
    flightDate: new Date(`2017-07-${Math.floor(i / 2) + 1}`),//航班日期
    segment:'航段',//航段
    serviceCity:'服务航站',//服务航站
    paxName:'旅客姓名',//旅客姓名
    pkgNo:' 行李号',//行李号
    payMoney:'补偿总额',//补偿总额
    payCount:'补偿单数量',//补偿单数量
    receiveStatus:'领取状态',//领取状态
    orderStatus:'补偿单状态',//补偿单状态
    pkgCount:'行李箱',//行李箱
    pkgSize:'尺寸',//尺寸
    dataType:'数据导入',//数据导入
    createUser:'创建人',//创建人
    auditor:'审核人',//审核人
    grantUser: '发放人',//发放人
    createDate:'创建时间',//创建时间
  });
}

function getRule(req, res, u) {
  let url = u;

  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    // eslint-disable-next-line prefer-destructuring
    url = req.url;
  }

  const params = parse(url, true).query;
  let dataSource = tableListDataSource;

  if (params.sorter) {
    const s = params.sorter.split('_');
    dataSource = dataSource.sort((prev, next) => {
      if (s[1] === 'descend') {
        return next[s[0]] - prev[s[0]];
      }

      return prev[s[0]] - next[s[0]];
    });
  }

  if (params.status) {
    const status = params.status.split(',');
    let filterDataSource = [];
    status.forEach(s => {
      filterDataSource = filterDataSource.concat(
        dataSource.filter(item => {
          if (parseInt(`${item.status}`, 10) === parseInt(s.split('')[0], 10)) {
            return true;
          }

          return false;
        }),
      );
    });
    dataSource = filterDataSource;
  }

  if (params.name) {
    dataSource = dataSource.filter(data => data.name.includes(params.name || ''));
  }

  let pageSize = 10;

  if (params.pageSize) {
    pageSize = parseInt(`${params.pageSize}`, 0);
  }

  const result = {
    data: dataSource,
    total: dataSource.length,
    success: true,
    pageSize,
    current: parseInt(`${params.currentPage}`, 10) || 1,
  };
  return res.json(result);
}

function postRule(req, res, u, b) {
  let url = u;

  if (!url || Object.prototype.toString.call(url) !== '[object String]') {
    // eslint-disable-next-line prefer-destructuring
    url = req.url;
  }

  const body = (b && b.body) || req.body;
  const { method, name, desc, key } = body;

  switch (method) {
    /* eslint no-case-declarations:0 */
    case 'delete':
      tableListDataSource = tableListDataSource.filter(item => key.indexOf(item.key) === -1);
      break;

    case 'post':
      const i = Math.ceil(Math.random() * 10000);
      tableListDataSource.unshift({
        key: i,
        key: i,
        accidentStatus: '处理中',//状态
        accidentType:'内件缺失',//类型
        flightNo:'CA123',//航班号
        flightDate: new Date(`2017-07-${Math.floor(i / 2) + 1}`),//航班日期
        segment:'航段',//航段
        serviceCity:'服务航站',//服务航站
        paxName:'旅客姓名',//旅客姓名
        pkgNo:' 行李号',//行李号
        payMoney:'补偿总额',//补偿总额
        payCount:'补偿单数量',//补偿单数量
        receiveStatus:'领取状态',//领取状态
        orderStatus:'补偿单状态',//补偿单状态
        pkgCount:'行李箱',//行李箱
        pkgSize:'尺寸',//尺寸
        dataType:'数据导入',//数据导入
        createUser:'创建人',//创建人
        auditor:'审核人',//审核人
        grantUser: '发放人',//发放人
        createDate:'创建时间',//创建时间
      });
      break;

    case 'update':
      tableListDataSource = tableListDataSource.map(item => {
        if (item.key === key) {
          return { ...item, desc, name };
        }

        return item;
      });
      break;

    default:
      break;
  }

  const result = {
    list: tableListDataSource,
    pagination: {
      total: tableListDataSource.length,
    },
  };
  return res.json(result);
}

export default {
  'GET /api/luggage/index': getRule,
  'POST /api/luggage/index': postRule,
};
